#include "dlg_bookaddandupdate.h"
#include "ui_dlg_bookaddandupdate.h"

Dlg_BookADDAndUpdate::Dlg_BookADDAndUpdate(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::Dlg_BookADDAndUpdate)
{
    ui->setupUi(this);
}

Dlg_BookADDAndUpdate::~Dlg_BookADDAndUpdate()
{
    delete ui;
}

void Dlg_BookADDAndUpdate::setType(int id,QStringList BookInfo)
{
    m_id=id;
    ui->le_name->setText(BookInfo[1]);
    ui->le_count->setText(BookInfo[2]);
    ui->cb1->setCurrentText(BookInfo[3]);
    ui->cb2->setCurrentText(BookInfo[4]);
    ui->le_price->setText(BookInfo[5]);
    //执行sql语句获取图书信息,并且显示到对应的控件上
    /*
    auto l=Sqlmgr::getinstance()->getBook(QString("where bookid=%1").arg(id));
    if(1==l.size())
    {
        auto data=l[0];
        ui->le_name->setText(data[1]);
        ui->le_count->setText(data[2]);
        ui->cb1->setCurrentText(data[3]);
        ui->cb2->setCurrentText(data[4]);
        ui->le_price->setText(data[5]);
    }
    */

}

void Dlg_BookADDAndUpdate::on_btn_ok_clicked()
{
    QStringList l;
    l<<QString::number(m_id);
    l<<ui->le_name->text();
    l<<ui->le_count->text();
    l<<ui->cb1->currentText();
    l<<ui->cb2->currentText();
    l<<ui->le_price->text();
    if(-1!=m_id)
    {
        //更改
        Sqlmgr::getinstance()->updateBook(l);
    }
    else
    {
        //添加
        QVector<QStringList> v;
        v.push_back(l);
        Sqlmgr::getinstance()->addBook(v);
    }
    this->hide();
}


void Dlg_BookADDAndUpdate::on_btn_cancel_clicked()
{
    this->hide();
}

